<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/Lib/Base.php';
require_once __DIR__ . '/funcs.php';
require_once __DIR__ . '/Lib/Page.php';

checkLogin();
checkFunc(8);

$now = time();
$success = $_GET['success'] ?? null;
$error = $_GET['error'] ?? null;
$action = $_GET['action'] ?? null;

$configs = $DB->select('game_config', '*', ['ORDER'=>['id'=>'ASC']]);

$action = $_GET['action'] ?? null;
if($action == 'edit') {
    checkFunc(9, '权限不足，无法操作');
    $cid = $_POST['id'] ?? null;
    $val = $_POST['val'] ?? '';

    if(!$cid || !$val) {
        echo 'BAD|参数错误';
        exit;
    }
    $DB->update('game_config', ['val'=>$val], ['id'=>$cid]);
    operate_log('config', 'edit_config', ['配置ID'=>$cid, '配置值'=>$val]);
    $all_config = $DB->select('game_config', '*');
    $td = [];
    foreach($all_config as $t) {
        $t['id'] = intval($t['id']);
        $t['Name'] = $t['name'];
        $t['Val'] = $t['val'];
        unset($t['name']);
        unset($t['val']);
        $td[$t['id']] = json_encode($t);
    }
    $REDIS->del('CONFIG_DATA');
    $REDIS->hmset('CONFIG_DATA', $td);
    echo 'OK';
    exit;
}

display_header();
?>
<link href="css/jquery.dm-uploader.min.css" rel="stylesheet">
<link href="js/trumbowyg/ui/trumbowyg.min.css" rel="stylesheet">
<style>

</style>

<section class="content">

    <?php display_title('参数配置'); ?>

    <?php if($error) : ?>
        <div class="alert alert-warning alert-dismissible">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            <span><?php echo $error; ?></span>
        </div>
    <?php endif ?>
    <?php if($success) : ?>
        <div class="alert alert-success alert-dismissible">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            <h4><i class="icon fa fa-check"></i> 成功</h4>
            <?php echo $success; ?>
        </div>
    <?php endif ?>

    <div class="box box-warning">
        <div class="box-header with-border">
            <h3 class="box-title"><i class="fa fa-tasks"></i> 参数配置</h3>
        </div>
        <div class="box-body">
            <table id="logtable" class="table table-bordered table-striped dataTable table-hover table-condensed" role="grid" aria-describedby="example1_info">
                <thead>
                <tr>
                    <th class="center" width="60">ID</th>
                    <th class="center" width="150">参数名</th>
                    <th class="center" width="200">参数介绍</th>
                    <th class="center">值</th>
                    <th class="center" width="200">操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach($configs as $cfg) {
                    if($cfg['name'] == 'goodsRule') continue;
                    ?>
                    <tr role="row" style="height:38px;text-align:center;">
                        <td class="">
                            <span class="cid" style="font-size:16px;font-weight:bold;"><?php echo $cfg['id']; ?></span>
                        </td>
                        <td class="cname"><?php echo $cfg['name']; ?></td>
                        <td class="cdesc"><?php echo $cfg['desc']; ?></td>
                        <td class="">
                            <?php
                            if($cfg['name'] == 'season') {
                                ?>
                                <select class="form-control cval" id="season_val">
                                    <option value="1" <?php if($cfg['val']==1) echo 'selected'; ?>>春</option>
                                    <option value="2" <?php if($cfg['val']==2) echo 'selected'; ?>>夏</option>
                                    <option value="3" <?php if($cfg['val']==3) echo 'selected'; ?>>秋</option>
                                    <option value="4" <?php if($cfg['val']==4) echo 'selected'; ?>>冬</option>
                                </select>
                                <?php
                            } else if(in_array($cfg['name'], ['couponRule'])) {
                                ?>
                                    <div class="cval bigeditbox"><?php echo $cfg['val']; ?></div>
                                <!-- <textarea class="form-control cval" id="text<?php echo $cfg['id'] ?>" style="height: 250px;width: 900px" name="" cols="30" rows="10"><?php echo $cfg['val']; ?></textarea> -->
                                <?php
                            }else if ($cfg['name'] == 'invite_link'){ ?>
                                <textarea class="form-control cval" style="width: 1200px" name="" cols="30" rows="10"><?php echo $cfg['val']; ?></textarea>
                            <?php } ?>
                        </td>
                        <td class="left" style="padding:0;padding-left:15px;" tid="<?php echo $cfg['id']; ?>">
                            <button class="btn btn-primary btn-sm edit_config">确认修改</button>
                        </td>
                    </tr>
                <?php } ?>
                </tbody>
            </table>
        </div>
    </div>

</section>

<!-- jQuery 3 -->
<script src="js/jquery.min.js"></script>
<script src="js/jquery.cookie.js"></script>
<script src="js/pace.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- Select2 -->
<script src="bower_components/select2/dist/js/select2.full.min.js"></script>
<!-- InputMask -->
<script src="plugins/input-mask/jquery.inputmask.js"></script>
<script src="plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
<script src="plugins/input-mask/jquery.inputmask.extensions.js"></script>
<!-- SlimScroll -->
<script src="bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<!-- iCheck 1.0.1 -->
<script src="plugins/iCheck/icheck.min.js"></script>
<!-- FastClick -->
<script src="bower_components/fastclick/lib/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.min.js"></script>

<script src="js/trumbowyg/trumbowyg.min.js"></script>
<script type="text/javascript" src="js/trumbowyg/langs/zh_cn.min.js"></script>

<script type="text/javascript">
    $(function() {
        $('.edit_config').click(function() {
            var cid = $(this).parent().parent().find('.cid').text();
            if(cid == '1' || cid== '4') {
                var val = $(this).parent().parent().find('.cval').val();
            } else {
                var val = $(this).parent().parent().find('.cval').html();
            }
            //val = val.replace(/\n|\r\n/g,'<br>');

            $.ajax({
                type: 'POST',
                url: 'game_config.php?action=edit',
                data: {id:cid, val:val},
                success: function(data) {
                    if(data != 'OK') {
                        var darr = data.split('|');
                        alert(darr[1]);
                    } else {
                        alert('修改成功');
                        location.reload();
                    }
                }
            });

            //location.href = 'game_config.php?action=edit&id=' + cid + '&val=' + val;
        });

        $('.bigeditbox').trumbowyg({
            lang: 'zh_cn',
        });

        setTimeout(function() {
            $('.alert-success').fadeOut();
            if($('.alert-warning').find('span').html() && $('.alert-warning').find('span').html().length < 60) {
                $('.alert-warning').fadeOut();
            }
        }, 2000);

    });
</script>

</body>
</html>
